package service

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"log"
	"net/http"
	"path"
	"resr_fun-api/pkg"
	"strings"
)

func UploadFile(c *gin.Context) {
	// 文件上传
	file, _ := c.FormFile("file")

	etc := strings.ToLower(path.Ext(file.Filename))
	if !strings.Contains(".mp4.avi.mkv", etc) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "仅支持.mp4、.avi、.mkv格式",
		})
		return
	}

	if file.Size > 5*1024*1024*1024 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "单个视频文件最大支持5GB",
		})
		return
	}

	file.Filename = uuid.NewString() + etc

	log.Println(file.Filename)

	dst := "../../file" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)

	//上传接口
	uploadFile := pkg.UploadFile(dst)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": uploadFile,
	})
}
